<template>
  <div class="container">
    <div class="app-container">
      <!-- 右上角的按钮 -->
      <div style="padding: 10px;">
        <el-button type="primary" size="mini" @click="addClisk">添加角色</el-button>
      </div>
      <!-- 中间的表格 -->
      <el-table :data="roleList">
        <!-- 第一列角色 -->
        <el-table-column prop="name" label="角色" align="center" width="200px">
          <template #default="{row}">
            <el-input v-if="row.isEdit" v-model="row.editRow.name" size="mini" />
            <span v-else>{{ row.name }}</span>
          </template>
        </el-table-column>

        <!-- 第二列判断启用 -->
        <el-table-column prop="state" label="启用" align="center" width="200px">
          <template v-slot:default="{row}">
            <el-switch v-if="row.isEdit" v-model="row.editRow.state" :active-value="1" :inactive-value="0" />
            <span v-else>{{ row.state === 1? '已启用': row.state === 0 ? '未启用':'其他' }}</span>
          </template>
        </el-table-column>

        <!-- 第三列描述 -->
        <el-table-column prop="description" label="描述" align="center">
          <template #default="{row}">
            <el-input v-if="row.isEdit" v-model="row.editRow.description" type="textarea" size="mini" :rows="2" />
            <span v-else>{{ row.description }}</span>
          </template>
        </el-table-column>

        <!-- 操作按钮 -->
        <el-table-column label="操作" align="center">
          <template #default="{row}">
            <div v-if="row.isEdit">
              <el-button size="mini" type="primary" @click="qrClick(row)">确认</el-button>
              <el-button size="mini" @click="row.isEdit=false">取消</el-button>
            </div>

            <div v-else>
              <el-button type="text" size="mini">分配权限</el-button>
              <el-button type="text" size="mini" @click="bjClick(row)">编辑</el-button>
              <el-popconfirm
                title="您确定删除吗？"
                @onConfirm="delConfirmClick(row)"
              >
                <el-button slot="reference" type="text" size="mini">删除</el-button>
              </el-popconfirm>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <!-- 右下角：角色分页器 -->
      <el-row style="height: 40;" type="flex" justify="end" align="middle">
        <el-pagination
          layout="prev, pager, next"
          :total="pageParams.total"
          :page-size="pageParams.pagesize"
          :current-page="pageParams.page"
          @current-change="currentChange"
        />
      </el-row>
    </div>
    <!-- 弹层组件 -->
    <el-dialog title="新增角色" :visible.sync="showDIalog" width="500px" @close="CloseClisk">
      <!-- 角色表单 -->
      <el-form ref="roleFrom" :model="roleFrom" :rules="ruleRules">

        <el-form-item label="角色名称" label-width="100px" prop="name">
          <el-input v-model="roleFrom.name" size="mini" style="width: 300px;" />
        </el-form-item>

        <el-form-item label="启用" label-width="100px" prop="state">
          <el-switch v-model="roleFrom.state" :active-value="1" :inactive-value="0" />
        </el-form-item>

        <el-form-item label="角色描述" label-width="100px" prop="description">
          <el-input v-model="roleFrom.description" :rows="3" size="mini" type="textarea" style="width: 300px;" />
        </el-form-item>

        <el-form-item label-width="100px">
          <el-row type="flex" justify="center" style="width: 300px;">
            <el-button size="mini" type="primary" @click="qdClick">确定</el-button>
            <el-button size="mini" @click="CloseClisk">取消</el-button>
          </el-row>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>
<script>
import { getRoleApi, postRoleApi, updatedRoleApi, delRoleApi } from '@/api/role'
export default {
  name: 'Role',
  data() {
    return {
      // 保存获取分业用户信息
      roleList: [],
      pageParams: {
        page: 1, // 当前页码
        pagesize: 5, // 一页显示几个
        total: 0 // 一共多少条数据
      },
      // 控制表单打开关闭
      showDIalog: false,
      // 绑定表单的数据
      roleFrom: {
        name: '',
        description: '',
        state: 1
      },
      // 校验的规则
      ruleRules: {
        name: [
          { required: true, message: '角色名称不能为空', trigger: 'blur' }
        ],
        description: [
          { required: true, message: '角色名称不能为空', trigger: 'blur' }
        ],
        state: [
          { required: true, message: '角色名称不能为空', trigger: 'blur' }
        ]
      }
    }
  },

  created() {
    // 页面渲染之前获取用户分页信息
    this.getRoleList()
  },
  methods: {
    // 删除
    async delConfirmClick(row) {
      await delRoleApi(row.id)
      this.$message.success('删除成功')
      if (this.roleList.length === 1 && this.pageParams.page > 1) {
        this.pageParams.page--
      }
      this.getRoleList()
    },

    // 编辑确定按钮
    async qrClick(row) {
      if (row.editRow.name && row.editRow.description) {
        // 调接口获取数据
        await updatedRoleApi({ ...row.editRow, id: row.id })

        // 提示用户修改成功
        this.$message.success('修改成功')

        // 更新数据                          退出编辑模式
        Object.assign(row, { ...row.editRow, isEdit: false })
      } else {
        this.$message.success('用户和描述不能为空')
      }
    },

    // 编辑
    bjClick(row) {
      row.isEdit = true
      // 用户再次点击编辑，获取取消之后，要重新赋值覆盖之前的值
      row.editRow.name = row.name
      row.editRow.description = row.description
      row.editRow.state = row.state
    },

    // 分页
    currentChange(currentPage) {
      this.pageParams.page = currentPage
      this.getRoleList()
    },
    // 确定
    qdClick() {
      this.$refs.roleFrom.validate(async(isOk) => {
        if (isOk) {
          await postRoleApi(this.roleFrom)
          this.$message.success('增加成功')
          this.getRoleList()
          this.CloseClisk()
        }
      })
    },

    //  取消
    CloseClisk() {
      this.$refs.roleFrom.resetFields()
      this.showDIalog = false
    },

    // 点击添加弹出弹层
    addClisk() {
      this.showDIalog = true
    },

    // 封装获取用户分页信息的方法
    async getRoleList() {
      const { rows, total } = await getRoleApi(this.pageParams)
      this.roleList = rows
      this.pageParams.total = total
      // 需要行内编辑所以需要给每一个数据对象增加一个标记
      // 遍历每一个对象加一个isEdit = false 的属性
      // 因为后加的数据不是响应式数据所以需要使用this.$set增加
      this.roleList.forEach((item) => {
        this.$set(item, 'isEdit', false)

        // 准备了两份数据让用户修改备份数据
        this.$set(item, 'editRow', {
          name: item.name,
          description: item.description,
          state: item.state
        })
      })
    }
  }
}
</script>
